package cn.ctyun.ipush.dao;

import cn.ctyun.ipush.model.YixinTokenModel;
import cn.ctyun.ipush.utils.DateUtils;
import com.alibaba.fastjson.JSON;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Liu
 * Date: 2016/7/22
 * Time: 9:14
 * To change this template use File | Settings | File Templates..
 */
@Component
public class YixinTokenDao {

    @Autowired
    private JdbcDao jdbcDao;

    /* 日志对象 */
    private final Logger LOG = LoggerFactory.getLogger(YixinTokenDao.class);
    /**
     * YixinToken 插入
     *
     * @param yixinTokenModel
     * @return
     */
    public boolean insert(YixinTokenModel yixinTokenModel) {

        try {
            jdbcDao.save(yixinTokenModel);
            LOG.info("task : " + JSON.toJSONString(yixinTokenModel) + " save success");
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 更新YixinToken
     *
     * @param yixinTokenModel
     * @return
     */
    public int update(YixinTokenModel yixinTokenModel) {
        if (yixinTokenModel == null) {
            LOG.info("Error yixinTokenModel id 更新不能为空");
            return 0;
        }
        yixinTokenModel.setModifyDate(DateUtils.dateToString(new Date()));
        return jdbcDao.update(yixinTokenModel);
    }

//    /**
//     * @param yixinModelId
//     * @return
//     */
//    public int delete(String yixinModelId) {
//        if (StringUtils.isEmpty(yixinModelId)) {
//            LOG.info("Error yixinModel id 更新不能为空");
//            return 0;
//        }
//        Criteria criteria=Criteria.update(YixinModel.class).set("isDeleted","1").where("yixinModelId",new Object[]{yixinModelId});
//        return jdbcDao.update(criteria);
//    }

    public YixinTokenModel querySingleResult(YixinTokenModel yixinTokenModel) {
        return jdbcDao.querySingleResult(yixinTokenModel);
    }
    public YixinTokenModel querySingleResult(String yixinTokenModelId) {
        if (StringUtils.isEmpty(yixinTokenModelId))
            throw new RuntimeException("yixinTokenModelId 主键id 必须设置");
        Criteria criteria = Criteria.select(YixinTokenModel.class).where("yixinTokenModelId", new Object[]{yixinTokenModelId});
        return jdbcDao.querySingleResult(criteria);
    }
//    public List<YixinModel> queryList() {
//        return jdbcDao.queryList(Criteria.select(YixinModel.class));
//    }
//
//    public List<YixinModel> queryList(Criteria criteria) {
//        return jdbcDao.queryList(criteria);
//    }
}
